package com.game;
import java.util.ArrayList;
import java.util.Arrays;

public class Player {
	String name = "";
	ArrayList<Card> hand;

	public Player(String name) {
		hand = new ArrayList<Card>();
		this.name = name;
	}
	
	/** return score of cards in player hand. */ 
	public int getScore() {
		int score = 0;
		for (int i = 0; i < hand.size(); i++) {
			score += hand.get(i).value;
		}
		return score % 10;
	}

	/** return bounce value if cards in player hand are same suit. */
	public int getBounce() {
		int bounces = 1;
		for (int i = 1; i < hand.size(); i++) {
			if (hand.get(i - 1).suit.equals(hand.get(i).suit))
				bounces++;
			else
				return 1;
		}
		return bounces;
	}

	/** return player name. */
	public String getName() {
		return this.name;
	}

	/** return sum of cards value in player hand. */
	public int getHandValue() {
		int kinds = 0;
		for (int i = 0; i < hand.size(); i++) {
			kinds += hand.get(i).kind;
		}
		return kinds;
	}

	/** check win type if get special win return each value else return 0. */
	public int specialWin() {
		if (hand.size() == 3) {
			if (isStraight() && getBounce() == 3) {
				return 40;
			} else if (isThreeKinds()) {
				return 30;
			} else if (isTile()) {
				return 20;
			} else if  (isStraight()) {
				return 10;
			} else {
				return 0;
			}
		} else {
			return 0;
		}
	}

	/** check cards in player hand if straight rank return true. */
	public boolean isStraight() {
		int[] temp = new int[3];
		for (int i = 0; i < temp.length; i++) {
			temp[i] = hand.get(i).kind;
		}
		Arrays.sort(temp);
		if (temp[2] - temp[1] == 1 && temp[1] - temp[0] == 1) {
			return true;
		}
		return false;
	}

	/** check cards in player hand if tile cards return true. */
	public boolean isTile() {
		for (int i = 0; i < hand.size(); i++) {
			if (hand.get(i).kind > 13 || hand.get(i).kind < 11)
				return false;
		}
		return true;
	}

	/** check cards in player hand if three of kinds return true. */
	public boolean isThreeKinds() {
		int temp = hand.get(0).kind;
		for (int i = 1; i < 3; i++) {
			if (hand.get(i).kind != temp)
				return false;
		}
		return true;
	}

	/** add one card from deck to hand */
	public boolean draw(Deck deck) {
		return hand.add(deck.draw());
	}
}
